<?php
declare (strict_types=1);

namespace app\api\controller;


use app\api\model\Mp as Mpmodel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\View;

class Mp extends Base
{
    /*
    订单主页
    */
    public function index()
    {
        return view();
    }

    /**
     * User: check
     * Notes:查看公众号的详细接入配置信息
     * @return string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function check()
    {
        $id = $this->request->param('id');
        if ($id) {
            $Mp = Mpmodel::where(['id' => $id])->find();
            $data['url'] = $this->request->domain() . Url('api/wechat/index', ['id' => $id]);
            $data['valid_token'] = $Mp['valid_token'];
            $data['encodingaeskey'] = $Mp['encodingaeskey'];
            view::assign('mp', $data);
            return view();
        } else {
            return '实践中';
        }

    }

    /**
     * @return array|\think\response\View
     */
    public function add()
    {

        if ($this->request->isPost()) {

            $name = $this->request->param('mp_name');
            $user = $this->request->param('mp_user');

            $appid = $this->request->param('appid');
            $appsecret = $this->request->param('appsecret');

            $uid = 3;
            //保存数据
            $Mpmodel = new Mpmodel;


            return $Mpmodel->add_one_data($name, $user, $appid, $appsecret, $uid);


        }

        return view();


    }


    public function del()
    {
        $id = $this->request->param('id');
        $Mpmodel = new Mpmodel;
        $uid = 3;

            $data = $Mpmodel->del_one_data($id, $uid);


        return $data;

    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data['id']=$this->request->param('id');
            $data['mp_name']=$this->request->param('mp_name');
            $data['mp_user']=$this->request->param('mp_user');
            $data['appid']=$this->request->param('appid');
            $data['appsecret']=$this->request->param('appsecret');
            $Mpmodel = new Mpmodel;
            return  $Mpmodel->edit_one_data($data);

        } else {
            $id = $this->request->param('id');
            if ($id) {
                $Mp = Mpmodel::where(['id' => $id])->find();
                $data['mp_name'] = $Mp['mp_name'];
                $data['mp_user'] = $Mp['mp_user'];
                $data['appid'] = $Mp['appid'];
                $data['appsecret'] = $Mp['appsecret'];
                view::assign('mp', $Mp);
                return view();
            } else {
                return '参数错误';
            }

        }


    }


}